package cn.fl.settlement.service.impl;

import cmm.comm.facade.upload.api.ISysUlDatCfgDtlApiService;
import cmm.comm.facade.upload.api.IUploadApiService;
import cmm.comm.facade.upload.condition.SysUlDatCfgDtlQueryCondition;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cmm.comm.facade.upload.dto.SysUlDatCfgDtlDTO;
import cmm.comm.facade.upload.dto.SysUlFilesRecDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.project.constant.PrjPrdPaySchMConstant;
import cn.fl.project.vo.PrjFileInfoVO;
import cn.fl.settlement.service.ISettlementFileService;
import cn.fl.upload.constant.UploadConstant;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @Description:    结清附件接口实现类
* @Author:         liurong
* @CreateDate:     2020/9/2 18:12
* @Version:        1.0
*/
@Service
public class SettlementFileServiceImpl implements ISettlementFileService {

    @Autowired
    private ISysUlDatCfgDtlApiService sysUlDatCfgDtlApiService;

    @Autowired
    private IUploadApiService uploadApiService;

    @Override
    public List<PrjFileInfoVO> searchSettleFileList(Long paySchId) throws BaseException {
        SysUlDatCfgDtlQueryCondition condition = new SysUlDatCfgDtlQueryCondition();
        condition.setIsDel(BaseConstant.IS_YESNO_NO);
        condition.setCfgCd(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_FILE_CODE);
        List<SysUlDatCfgDtlDTO> cfgDtlList = sysUlDatCfgDtlApiService.srearchSyUlDatCfgDtlList(condition);

        if (CollectionUtils.isEmpty(cfgDtlList)) {
            return Lists.newArrayList();
        }

        SysUlFilesRecQueryCondition fileCondition = new SysUlFilesRecQueryCondition();
        fileCondition.setPkId(String.valueOf(paySchId));
        fileCondition.setSysCd(WebBaseConstant.SYS_SYS_CD);
        fileCondition.setCfgCd(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_FILE_CODE);
        List<SysUlFilesRecDTO> sysUlFilesRecDTOS = uploadApiService.searchSysUlFilesRecList(fileCondition);
        int fileSize = 0;
        if (CollectionUtils.isNotEmpty(sysUlFilesRecDTOS)) {
            fileSize = sysUlFilesRecDTOS.size();
        }
        List<PrjFileInfoVO> fileList = Lists.newArrayListWithCapacity((cfgDtlList.size() + fileSize));

        String picServicePath = PropertiesCacheUtils.getProperty(UploadConstant.UPLOAD_SERVER_PATH);

        boolean f;
        for (SysUlDatCfgDtlDTO datCfgDtl : cfgDtlList) {
            f = true;
            for (SysUlFilesRecDTO filesRecDTO : sysUlFilesRecDTOS) {
                if (datCfgDtl.getCfgDtlCd().equals(filesRecDTO.getCfgDtlCd())) {
                    PrjFileInfoVO vo = createFileInfoVo(datCfgDtl, filesRecDTO, picServicePath, paySchId);
                    fileList.add(vo);

                    f = false;
                }
            }
            if (f) {
                PrjFileInfoVO vo = createFileInfoVo(datCfgDtl, null, picServicePath, paySchId);
                fileList.add(vo);
            }
        }

        return fileList;
    }

    private PrjFileInfoVO createFileInfoVo(SysUlDatCfgDtlDTO datCfgDtl, SysUlFilesRecDTO filesRecDTO, String picServicePath, Long pkId) {
        PrjFileInfoVO vo = new PrjFileInfoVO();
        vo.setId(pkId);//业务主键
        vo.setCfgDtlCd(datCfgDtl.getCfgDtlCd());
        vo.setDatNm(datCfgDtl.getDatNm());
        vo.setCfgCd(datCfgDtl.getCfgCd());
        vo.setOrgCd(datCfgDtl.getOrgCd());
        vo.setMaxNum(datCfgDtl.getMaxNum());
        vo.setIsAuth(datCfgDtl.getIsAuth());
        vo.setIsDel(datCfgDtl.getIsDel());
        vo.setIsValid(datCfgDtl.getIsValid());
        vo.setIsMust(datCfgDtl.getIsMust());//是否必传
        if (filesRecDTO != null) {
            vo.setFileId(filesRecDTO.getId());//附件ID
            //如果有HTTP开头说明是可以访问路径,无需干涉
            if (StringUtils.isNotEmpty(filesRecDTO.getFilePath())
                    && !filesRecDTO.getFilePath().startsWith("http")
                    && StringUtils.isNotEmpty(picServicePath)) {
                vo.setFilePath(picServicePath + filesRecDTO.getFilePath());
            } else {
                vo.setFilePath(filesRecDTO.getFilePath());
            }
            vo.setFileNm(filesRecDTO.getFileNm());//上传名称
            vo.setCrtTm(filesRecDTO.getCrtTm());//上传时间
        }
        return vo;
    }

    @Override
    public boolean removeFile(Long fileId) throws BaseException {
        List<Long> idList = new ArrayList<>(1);
        idList.add(fileId);
        return this.uploadApiService.removeSysUlFilesRecByIds(idList);
    }

    @Override
    public boolean isUploadStlApplication(String pkId) throws BaseException {
        if (StringUtils.isEmpty(pkId)) {
            throw new BaseException("请求参数：数据主键ID不能为空！");
        }
        // 查询申请书配置
        SysUlDatCfgDtlQueryCondition condition = new SysUlDatCfgDtlQueryCondition();
        condition.setIsDel(BaseConstant.IS_YESNO_NO);
        condition.setCfgCd(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_FILE_CODE);
        condition.setCfgDtlCd(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_FILE_DETAIL_CODE);
        List<SysUlDatCfgDtlDTO> cfgDtlList = sysUlDatCfgDtlApiService.srearchSyUlDatCfgDtlList(condition);
        if (CollectionUtils.isEmpty(cfgDtlList)) {
            // 没有配置申请书，则返回true
            return true;
        }
        SysUlDatCfgDtlDTO sysUlDatCfgDtlDTO = cfgDtlList.get(0);
        // 判断是否必传
        if (sysUlDatCfgDtlDTO.getIsMust() == BaseConstant.IS_YESNO_YES) {
            SysUlFilesRecQueryCondition fileCondition = new SysUlFilesRecQueryCondition();
            fileCondition.setPkId(pkId);
            fileCondition.setCfgDtlCd(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_FILE_DETAIL_CODE);
            fileCondition.setIsDel(BaseConstant.IS_YESNO_NO);
            fileCondition.setSysCd(WebBaseConstant.SYS_SYS_CD);
            fileCondition.setCfgCd(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_FILE_CODE);
            fileCondition.setTabNm("PRJ_PRD_PAY_SCH_M");
            List<SysUlFilesRecDTO> sysUlFilesRecDTOS = uploadApiService.searchSysUlFilesRecList(fileCondition);
            if (CollectionUtils.isEmpty(sysUlFilesRecDTOS)) {
                return false;
            }
        }
        return true;
    }
    




}
